package com.retailshop.shopuser.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopuser.entity.RedisUserNo;
import com.retailshop.shopuser.entity.ShareUser;
import com.retailshop.shopuser.mapper.RedisUserNoMapper;
import com.retailshop.shopuser.service.UserShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("UserShareService")
public class UserShareServiceImpl implements UserShareService {

    @Autowired
    RedisUserNoMapper redisUserNoMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public int FinishingUserShareAll(Integer id) {

        SetMsg("shareMsg","开始整理所有编号分享市场信息");
        SetState("shareState","1");

        //读取所有用户信息
        List<RedisUserNo> redisUserNoList = redisUserNoMapper.getShareUserAll(id);

        if(redisUserNoList!=null)
        {
            for (int i=0;i<redisUserNoList.size();i++) {
                if (!GetState("shareControl")) {
                    //整理编号
                    UserShare(redisUserNoList.get(i));
                } else {
                    break;
                }
            }
        }

        SetState("shareState","0");

        return 1;
    }

    @Override
    public int FinishingUserShareMinId() {

        //获取数据库中最大的ID
        int id =0;

        try {
            id = redisUserNoMapper.getMaxId();
        }catch (Exception ex)
        {}

        FinishingUserShareAll(id);

        return 1;
    }

    @Override
    public int FinishingUserShare(Integer id) {

        SetMsg("shareMsg","开始整理所有编号分享市场信息");
        SetState("shareState","1");

        //读取用户信息
        RedisUserNo redisUserNo = redisUserNoMapper.getShareUser(id);

        if(redisUserNo!=null)
        {
            if(!GetState("shareControl")) {
                UserShare(redisUserNo);
            }
        }

        SetState("shareState","0");

        return 1;
    }

    public int UserShare(RedisUserNo redisUserNo) {
        SetMsg("shareMsg","开始整理："+redisUserNo.getId());
        int shareId = redisUserNo.getShare();
        System.out.println("shareId："+shareId);
        if(shareId>0)
        {
            List<ShareUser> shareUsers = new ArrayList();
            int cs=1;

            RedisUserNo redisUserNo1 =redisUserNo;

            //循环查找上级
            while (shareId>0 && redisUserNo1!=null)
            {
                //添加记录
                System.out.println("添加记录："+redisUserNo1.getId());
                shareUsers.add(new ShareUser(redisUserNo.getId(),redisUserNo1.getShare(),cs));

                System.out.println("读取用户信息ID:" + shareId);
                //SetMsg("shareMsg","读取用户信息ID:" + shareId);
                /*读取用户信息*/
                Object obj = redisTemplate.opsForValue().get("ID" + shareId);
                if(obj!=null) {
                    redisUserNo1 = JSON.parseObject(obj.toString(), new TypeReference<RedisUserNo>() {
                    });
                }

                if (redisUserNo1 != null) {
                    shareId = redisUserNo1.getShare();
                    cs++;
                }
                else
                {
                    break;
                }
            }
            redisUserNoMapper.inShareUser(shareUsers);
        }
        //SetMsg("shareMsg","整理"+redisUserNo.getId()+"完成.");
        return 0;
    }

    public int UserShare1() {
        int minid = redisUserNoMapper.getMaxId();
        System.out.println("minid:"+minid);
        return 0;
    }

    @Override
    public void SetMsg(String key , String value)
    {
        System.out.println(key+":"+value);
        redisTemplate.opsForValue().set(key,value);
    }

    @Override
    public void SetState(String key , String value)
    {
        System.out.println(key+":"+value);
        redisTemplate.opsForValue().set(key,value);
    }

    @Override
    public String GetMsg(String key)
    {
        String value = redisTemplate.opsForValue().get(key)==null ? "":redisTemplate.opsForValue().get(key).toString();
        System.out.println(key+":"+value);
        return value;
    }

    @Override
    public boolean GetState(String key)
    {
        String value = redisTemplate.opsForValue().get(key)==null ? "":redisTemplate.opsForValue().get(key).toString();
        System.out.println(key+":"+value);
        if(value=="1" || value.equals("1"))
        {
            //System.out.println("控制");
            return true;
        }
        else {
            //System.out.println("非控制");
            return false;
        }
    }

}
